package com.eateateat.controller;

import com.eateateat.service.OrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController  // @Controller+@ResponseBody @ResponseBody注解来返回一个JSON对象
@RequestMapping("/order")
public class OrderController {
    Logger logger = Logger.getLogger(OrderController.class);

    @Autowired
    OrderService orderService;

@PostMapping("/select")
List select(HttpServletRequest request) throws IOException {

          Map m  = (Map) request.getSession().getAttribute("islogin");
            String lxmemid= (String) m.get("lxmemid");
    logger.debug(lxmemid);
    List result=orderService.select(lxmemid);
    logger.debug(result);
    return result;
}
    @GetMapping("/insert/{ids}") //order2插入
    String insert(HttpServletRequest request, @PathVariable String ids) throws IOException {
        logger.debug(ids);

        Map lxmemid = (Map) request.getSession().getAttribute("islogin");
        logger.debug(lxmemid);
        logger.debug(ids);
        try {
            if (orderService.insert(ids,(String)lxmemid.get("lxmemid"))){
                return "success";
            } else {
                return "failure";
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return "failure";
        }
    }

    @GetMapping("/delete")
    String delete(HttpServletRequest request) throws IOException {
        int xgorderid = Integer.parseInt(request.getParameter("xgorderid"));
        logger.debug(xgorderid);
        String xgmemid = (String) request.getSession().getAttribute("islogin");
        logger.debug(xgmemid);
        if (orderService.delete(xgmemid,xgorderid)){
            return "success";
        } else {
            return "failure";
        }
    }
    }

